package com.blog.admin.aspect;

import com.blog.admin.annotation.BuildCache;
import com.blog.admin.listener.event.CacheEvent;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * <p>
 * 重建fileCache
 * </p>
 *
 * @author Valerie
 * @date 2023/6/4
 **/
@Component
@Aspect
public class FileCacheAspect {

    @Resource
    private ApplicationEventPublisher eventPublisher;



    @Pointcut("@annotation(com.blog.admin.annotation.BuildCache)")
    public void pointCut(){
    }

    @AfterReturning("pointCut()")
    public void buildCache(JoinPoint joinPoint){
        MethodSignature methodSignature= (MethodSignature)joinPoint.getSignature();
        BuildCache buildCache = methodSignature.getMethod().getAnnotation(BuildCache.class);
        String key = buildCache.value();
        eventPublisher.publishEvent(new CacheEvent(joinPoint.getTarget(), key));
    }
}
